package com.heqiang.tools;


import java.util.Set;
import javax.validation.ConstraintViolation;

public class DataTableResultUtils {
    public DataTableResultUtils() {
    }

    public static DataTableResult returnError(String msg) {
        DataTableResult result = new DataTableResult();
        result.setCode(0);
        result.setInfo(msg);
        result.setSuccess(false);
        return result;
    }

    public static DataTableResult returnError(String msg, Object unwrappData) {
        DataTableResult result = new DataTableResult();
        result.setCode(0);
        result.setInfo(msg);
        result.setData(unwrappData);
        result.setSuccess(false);
        return result;
    }

    public static DataTableResult returnError(String msg, Integer code) {
        DataTableResult result = new DataTableResult();
        result.setCode(code);
        result.setInfo(msg);
        result.setSuccess(false);
        return result;
    }

    public static <T> DataTableResult returnError(Set<ConstraintViolation<T>> constraintViolations) {
        DataTableResult result = new DataTableResult();
        result.setCode(0);
        result.setInfo(((ConstraintViolation)constraintViolations.iterator().next()).getMessage());
        result.setSuccess(false);
        return result;
    }

    public static DataTableResult returnSuccess(String msg) {
        DataTableResult result = new DataTableResult();
        result.setCode(1);
        result.setInfo(msg);
        result.setSuccess(true);
        return result;
    }

    public static DataTableResult returnSuccess(String msg, Object object, Integer recordsTotal) {
        return returnSuccessResult(msg, object, recordsTotal, recordsTotal);
    }

    public static DataTableResult returnSuccess(String msg, Object object, Integer recordsTotal, Integer recordsTotalFiltered) {
        return returnSuccessResult(msg, object, recordsTotal, recordsTotalFiltered);
    }

    public static DataTableResult returnSuccess(String msg, Page page) {
        return returnSuccessResult(msg, page);
    }

    public static DataTableResult returnSuccess(Page page) {
        return returnSuccessResult("", page);
    }

    public static DataTableResult returnSuccess(Object object, Integer recordsTotal) {
        return returnSuccessResult("", object, recordsTotal, recordsTotal);
    }

    private static DataTableResult returnSuccessResult(String msg, Page page) {
        return returnSuccessResult(msg, page.getList(), page.getTotal(), page.getTotal());
    }

    private static DataTableResult returnSuccessResult(String msg, Object object, Integer recordsTotal, Integer recordsTotalFiltered) {
        DataTableResult result = new DataTableResult();
        result.setCode(1);
        result.setInfo(msg);
        result.setData(object);
        result.setSuccess(true);
        result.setRecordsTotal(recordsTotal);
        result.setRecordsFiltered(recordsTotalFiltered);
        return result;
    }
}
